package com.wcity.manage.web;

import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.model.Act;
import com.wcity.manage.dao.model.Role;
import com.wcity.manage.service.ActService;
import com.wcity.manage.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色管理控制层
 *
 * @author dy
 */
@Controller
public class RoleAct {

    /**
     * 角色列表
     *
     * @param role
     * @param pageModel
     * @param request
     * @return
     */
    @RequestMapping( "role_list" )
    public String roleList(Role role, PageModel pageModel, HttpServletRequest request ) {
        pageModel.setParams( role );
        request.setAttribute( "pageModel", pageModel );
        request.setAttribute( "role", role );
        request.setAttribute("list", roleService.getRoleListPage( pageModel ) );
        return "manage/rolelist";
    }

    /**
     * 跳转至添加角色页面
     *
     * @param request
     * @return
     */
    @RequestMapping( "to_role_add" )
    public String toRoleAdd( HttpServletRequest request ) {
        List<Act> list = actService.getActTreeList();
        request.setAttribute( "actTree", DwzUtil.getRoleTree( list, new StringBuffer() ) );
        return "manage/roleadd";
    }

    /**
     * 添加角色
     *
     * @param role
     * @param actTreeStr
     * @param request
     * @param response
     */
    @RequestMapping( "role_add" )
    public void roleAdd( Role role, String actTreeStr, HttpServletRequest request, HttpServletResponse response ) {

        List<Integer> authIds = new ArrayList<Integer>();
        if ( !StringUtil.isBlank( actTreeStr ) ) {
            String[] authIdStrArray = actTreeStr.split( "," );
            for ( String authIdStr : authIdStrArray ) {
                authIds.add( Integer.valueOf( authIdStr ) );
            }
        }

        String errorMsg = roleService.addRole( role, authIds );
        if ( StringUtil.isBlank( errorMsg ) ) {
            // 添加成功
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "添加成功", "role_list", "", "closeCurrent", "" ) );
        } else {
            // 添加失败
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }
    }

    /**
     * 跳转至修改角色页面
     *
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping( "to_role_edit" )
    public String toUserEdit( int id, HttpServletRequest request, HttpServletResponse response ) {
        Role role = roleService.getRole( id );
        if ( role == null ) {
            return "manage/roleedit";
        }
        role.setId( id );
        request.setAttribute( "role", role );
        List<Act> list = actService.getActTreeList();
        request.setAttribute( "actTree", DwzUtil.getRoleTree( list, new StringBuffer(), role ) );
        return "manage/roleedit";
    }

    /**
     * 修改角色
     *
     * @param id
     * @param role
     * @param actTreeStr
     * @param request
     * @param response
     */
    @RequestMapping( "role_edit" )
    public void roleEdit( int id, Role role, String actTreeStr, HttpServletRequest request, HttpServletResponse response ) {

        List<Integer> authIds = new ArrayList<Integer>();
        if ( !StringUtil.isBlank( actTreeStr ) ) {
            String[] authIdStrArray = actTreeStr.split( "," );
            for ( String authIdStr : authIdStrArray ) {
                authIds.add( Integer.valueOf( authIdStr ) );
            }
        }

        String errorMsg = roleService.updateRole( role, id, authIds );
        if ( StringUtil.isBlank(errorMsg) ) {
            // 添加成功
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "修改成功", "role_list", "", "closeCurrent", "" ) );
        } else {
            // 添加失败
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson("300", errorMsg, "", "", "", "") );
        }
    }

    @Resource private RoleService roleService;
    @Resource private ActService actService;

}
